/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>
#include <mc/common.h>
#include <mctp/mctp.h>
#include <ncsi_over_mctp/ncsi_over_mctp.h>

namespace {

// Test fixture
class ncsi_over_mctp_test : public ::testing::Test {
protected:
    void SetUp() override {
        m_mctp = mc::make_shared<dev::mctp>(nullptr, 0, dev::MCTP_MESSAGE_TYPE_NCSI, "");
        m_ncsi = std::make_shared<dev::ncsi_over_mctp>(*m_mctp);
    }

    void TearDown() override {
        m_ncsi.reset();
        m_mctp.reset();
    }

    mc::shared_ptr<dev::mctp>         m_mctp;
    std::shared_ptr<dev::ncsi_over_mctp> m_ncsi;
};

// ========== 构造函数测试 ==========

TEST_F(ncsi_over_mctp_test, Constructor) {
    // 目标：验证 ncsi_over_mctp 可以正常构造
    dev::mctp mctp_obj(nullptr, 0, dev::MCTP_MESSAGE_TYPE_NCSI, "");
    dev::ncsi_over_mctp ncsi(mctp_obj);
    EXPECT_TRUE(true);
}

TEST_F(ncsi_over_mctp_test, Destructor) {
    // 目标：验证 ncsi_over_mctp 可以正常析构
    {
        dev::mctp mctp_obj(nullptr, 0, dev::MCTP_MESSAGE_TYPE_NCSI, "");
        dev::ncsi_over_mctp ncsi(mctp_obj);
    }
    EXPECT_TRUE(true);
}

// ========== request 方法测试 ==========

TEST_F(ncsi_over_mctp_test, request_EmptyContext) {
    // 目标：验证 request 方法在上下文为空时返回 false
    mc::dict             ctx;
    std::vector<uint8_t> data = {0x01, 0x02};
    std::string          rsp;

    bool result = m_ncsi->request(ctx, data, rsp);
    EXPECT_FALSE(result);
}

TEST_F(ncsi_over_mctp_test, request_MissingPacketType) {
    // 目标：验证 request 方法在缺少 PacketType 时返回 false
    mc::dict             ctx{{"PackageId", static_cast<int64_t>(1)}};
    std::vector<uint8_t> data = {0x01, 0x02};
    std::string          rsp;

    bool result = m_ncsi->request(ctx, data, rsp);
    EXPECT_FALSE(result);
}

TEST_F(ncsi_over_mctp_test, request_WithPacketType) {
    // 目标：验证 request 方法在包含 PacketType 时调用 m_mctp.request
    mc::dict             ctx{{"PacketType", static_cast<int64_t>(1)}};
    std::vector<uint8_t> data = {0x01, 0x02};
    std::string          rsp;

    // 由于 m_mctp 未初始化 endpoint，request 应该返回 false
    bool result = m_ncsi->request(ctx, data, rsp);
    EXPECT_FALSE(result);
}

TEST_F(ncsi_over_mctp_test, request_EmptyData) {
    // 目标：验证 request 方法可以处理空数据
    mc::dict             ctx{{"PacketType", static_cast<int64_t>(1)}};
    std::vector<uint8_t> data;
    std::string          rsp;

    bool result = m_ncsi->request(ctx, data, rsp);
    EXPECT_FALSE(result);
}

TEST_F(ncsi_over_mctp_test, MultipleInstances) {
    // 目标：验证可以创建多个 ncsi_over_mctp 实例
    std::vector<std::shared_ptr<dev::ncsi_over_mctp>> ncsi_instances;

    for (int i = 0; i < 3; ++i) {
        auto mctp = mc::make_shared<dev::mctp>(nullptr, 0, dev::MCTP_MESSAGE_TYPE_NCSI, "");
        auto ncsi = std::make_shared<dev::ncsi_over_mctp>(*mctp);
        ncsi_instances.push_back(ncsi);
    }

    EXPECT_EQ(ncsi_instances.size(), 3);
}

} // namespace

